package org.fly.utils;

import org.codehaus.jackson.type.TypeReference;
import org.fly.bean.ProductNorm;
import org.fly.bean.SupplierProduct;
import org.fly.contants.WebConstant;
import org.fly.enums.PriceType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by rongzhao.zhu on 2018/4/10.
 */
public class PriceUtils {

    public static String calcPrice(SupplierProduct supplierProduct,
                             String customerLevel,
                             ProductNorm productNorm,
                             String norm){
        String supplierId = supplierProduct.getSupplierId();
        Map<String,String> priceMap = new HashMap<>();
        PriceUtils.mapPrice(priceMap,supplierProduct,customerLevel,productNorm);
        if(norm ==null){
            norm = "";
        }
        String key = supplierId+ WebConstant.splitStr+norm;
        //获取单价
        String singlePrice = priceMap.get(key);
        return singlePrice;
    }

    public static void mapPrice(Map<String,String> priceMap,
                                SupplierProduct supplierProduct,
                                String customerLevel,
                                ProductNorm productNorm){
        String priceType = supplierProduct.getPriceType();
        if(PriceType.same.name().equals(priceType)){
            mapPriceSame(priceMap,supplierProduct.getSupplierId(),productNorm,supplierProduct.getConfigJson());
        }else if(PriceType.norm.name().equals(priceType)){
            mapPriceNorm(priceMap,supplierProduct.getSupplierId(),supplierProduct.getConfigJson());
        }else if(PriceType.level.name().equals(priceType)){
            mapPriceLevel(priceMap,supplierProduct.getSupplierId(),customerLevel,productNorm,supplierProduct.getConfigJson());
        }else if(PriceType.normLevel.name().equals(priceType)){
            mapPriceNormLevel(priceMap,supplierProduct.getSupplierId(),customerLevel,supplierProduct.getConfigJson());
        }
    }
    /**
     *  供货商设置的是统一价格
     * @param mapPrice
     * @param supplierId
     * @param configJson
     */
    public static void mapPriceSame( Map<String,String> mapPrice,
                               String supplierId,ProductNorm productNorm ,
                               String configJson){
        Map<String,String> priceSetting = JsonUtils.decode(configJson, new TypeReference<HashMap<String,String>>() {
        });
        String price = priceSetting.get("price");
        if(productNorm == null){
            mapPrice.put(supplierId+ WebConstant.splitStr,price);
        }else {
            String normValues =  productNorm.getValues();
            String[] normValueArray =  normValues.split(",");
            Stream.of(normValueArray).forEach(normValue->{
                mapPrice.put(supplierId+ WebConstant.splitStr+normValue,price);
            });
        }
    }

    /**
     * 供货商设置的是规格不同价格不同
     * @param mapPrice
     * @param supplierId
     * @param configJson
     */
    public static void mapPriceNorm(Map<String,String> mapPrice,
                              String supplierId,
                              String configJson){
        ArrayList<HashMap<String,String>> _temp = JsonUtils.decode(configJson, new TypeReference<ArrayList<HashMap<String,String>>>() {
        });
        Map<String,String>priceSetting =  _temp.stream().collect(Collectors.toMap(e->e.get("norm"), e->e.get("price")));
        priceSetting.entrySet().forEach(e->{
            String  key =  e.getKey();
            String  value = e.getValue();
            mapPrice.put(supplierId+ WebConstant.splitStr+key,value);
        });
    }

    /**
     * 供货商配置 不同级别价格不一致
     * @param mapPrice
     * @param supplierId
     * @param customerLevel
     * @param productNorm
     * @param configJson
     */
    public static void mapPriceLevel(Map<String,String> mapPrice,
                               String supplierId,
                               String customerLevel,
                               ProductNorm productNorm ,
                               String configJson
    ){
        String price = "";
        ArrayList<HashMap<String,String>> _temp = JsonUtils.decode(configJson, new TypeReference<ArrayList<HashMap<String,String>>>() {
        });
        for(int index = 0;index<_temp.size();index++){
            Map<String,String> levelPrice =  _temp.get(index);
            String level =  levelPrice.get("level");
            String _price = levelPrice.get("price");
            if(level.equals(customerLevel)){
                price = _price;
            }

        }

        if(productNorm == null){
            mapPrice.put(supplierId+ WebConstant.splitStr,price);
        }else {
            String normValues =  productNorm.getValues();
            String[] normValueArray =  normValues.split(",");
            for(int i=0;i<normValueArray.length;i++){
                String normValue = normValueArray[i];
                mapPrice.put(supplierId+ WebConstant.splitStr+normValue,price);
            }
        }
    }

    public static void mapPriceNormLevel(Map<String,String> mapPrice,
                                   String supplierId,
                                   String customerLevel,
                                   String configJson
    ){
        ArrayList<HashMap<String,String>> _temp = JsonUtils.decode(configJson, new TypeReference<ArrayList<HashMap<String,String>>>() {
        });
        _temp.stream().forEach(e->{
            String level = e.get("level");
            String norm = e.get("norm");
            String price = e.get("price");
            if (level.equals(customerLevel)){
                mapPrice.put(supplierId+ WebConstant.splitStr+norm,price);
            }
        });

    }
}
